package com.hangwei.aicabinet.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangwei.aicabinet.App;
import com.hangwei.aicabinet.R;
import com.hangwei.aicabinet.manager.SerialPortProxy;
import com.hangwei.aicabinet.util.NotFastClickListener;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.ToastUtil;

/**
 * Created by 张龙臣 on 2018/10/31.
 */

public class AdminAdapter extends RecyclerView.Adapter<AdminAdapter.AdminHolder> {


    @NonNull
    @Override
    public AdminHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_admin, parent, false);
        return new AdminHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull AdminHolder holder, int position) {
        holder.init(position);
    }

    @Override
    public int getItemCount() {
        return 8;
    }

    class AdminHolder extends RecyclerView.ViewHolder {
        public AdminHolder(View itemView) {
            super(itemView);
        }

        public void init(final int position) {
            RelativeLayout rlView = itemView.findViewById(R.id.rlView);
            TextView textView = itemView.findViewById(R.id.tvNum);
            ImageView imageView = itemView.findViewById(R.id.ivWitch);
            TextView status = itemView.findViewById(R.id.tvStatus);
            textView.setText(position + 1 + "");

            switch (position) {
                case 0:
                    imageView.setImageResource(R.drawable.which_1);
                    break;
                case 1:
                    imageView.setImageResource(R.drawable.which_2);
                    break;
                case 2:
                    imageView.setImageResource(R.drawable.which_3);
                    break;
                case 3:
                    imageView.setImageResource(R.drawable.which_4);
                    break;
                case 4:
                    imageView.setImageResource(R.drawable.which_5);
                    break;
                case 5:
                    imageView.setImageResource(R.drawable.which_6);
                    break;
                case 6:
                    imageView.setImageResource(R.drawable.which_7);
                    break;
                case 7:
                    imageView.setImageResource(R.drawable.which_8);
                    break;
            }
            int witch = SPUtil.getInt(SPKey.Admin.OPEN_DOOR);
            if (witch == position+1) {
                status.setText("已打开");
                status.setTextColor(imageView.getContext().getResources().getColor(R.color.light));
                rlView.setBackgroundResource(R.color.wait);
            } else {
                status.setText("打开柜门");
                status.setTextColor(imageView.getContext().getResources().getColor(R.color.text));
                rlView.setBackgroundResource(R.color.admin_bg);
            }

            // 初始所有的柜子是关闭的，点击开门
            // 给对应锁发开锁指令，成功后
            // 1.打开锁监听 2.修改对应背景和文字 3.本地记录所开柜门（4.更新服务器锁状态）
            // 监听到柜门已关
            // 关闭锁监听 （更新服务器锁状态）  轮询（柜门还是整柜？） 上传标签
            // 收到服务器返回的上传结果 弹出提示框显示有哪些工具变动 是否确认
            //    确认      操作完成 本地开门记录清除 刷新界面
            //    认为有误  重新弹开此门
            itemView.setOnClickListener(new NotFastClickListener() {
                @Override
                public void isNotFastClick(View view) {
                    int anInt = SPUtil.getInt(SPKey.Admin.OPEN_DOOR);
                    if (anInt>0){
                        ToastUtil.showCenterToast("已有柜门打开");
                        return;
                    }
                    SerialPortProxy.openLock(position + 1);
                    App.tempLock = position + 1;//临时记录，等到开锁成功再记录到SP
                    Log.d(" App.tempLock","--------------------"+ App.tempLock);
                }

                @Override
                protected void doubleClick(View v) {
                    ToastUtil.showCenterToast("请勿连续点击开门");
                }
            });

        }
    }
}
